﻿package com.firewolf.proxy.jdk;

import java.lang.reflect.Proxy;

public class ProxyTest {
	public static void main(String[] args) {
		IUserDAO userDAO = new UserDAOImpl();
		
		//对userDAO对象做代理对象
		MyInvocationHandler handler = new MyInvocationHandler(userDAO);
		
		//newProxyInstance方法的前两个参数分别是被代理对象的类加载器和所有接口类
		//返回的还是被代理的对象，只不过做了额外的事情而已
		IUserDAO userDaoProxy = (IUserDAO) Proxy.newProxyInstance(userDAO.getClass().getClassLoader(), userDAO.getClass().getInterfaces(), handler);
		
		userDaoProxy.add();
		userDaoProxy.delete();
		userDaoProxy.update();
		userDaoProxy.find();
		
		//学生登录也可以使用这个代理
		IStudentDAO student = new StudentDAOImpl();
		MyInvocationHandler handler2 = new MyInvocationHandler(student);
		IStudentDAO studentDAOProxy = (IStudentDAO) Proxy.newProxyInstance(student.getClass().getClassLoader(), student.getClass().getInterfaces(), handler2);
		studentDAOProxy.register();
		studentDAOProxy.login();
	}
}
